<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>django.contrib.comments.forms &mdash; Django 1.7.8.dev20150401230226 documentation</title>
    
    <link rel="stylesheet" href="../../../../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../../../../_static/pygments.css" type="text/css" />
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../../../../',
        VERSION:     '1.7.8.dev20150401230226',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../../../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../../../_static/doctools.js"></script>
    <link rel="top" title="Django 1.7.8.dev20150401230226 documentation" href="../../../../index.html" />
    <link rel="up" title="django.contrib.comments" href="../comments.html" />



 
<script type="text/javascript" src="../../../../templatebuiltins.js"></script>
<script type="text/javascript">
(function($) {
    if (!django_template_builtins) {
       // templatebuiltins.js missing, do nothing.
       return;
    }
    $(document).ready(function() {
        // Hyperlink Django template tags and filters
        var base = "../../../../ref/templates/builtins.html";
        if (base == "#") {
            // Special case for builtins.html itself
            base = "";
        }
        // Tags are keywords, class '.k'
        $("div.highlight\\-html\\+django span.k").each(function(i, elem) {
             var tagname = $(elem).text();
             if ($.inArray(tagname, django_template_builtins.ttags) != -1) {
                 var fragment = tagname.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + tagname + "</a>");
             }
        });
        // Filters are functions, class '.nf'
        $("div.highlight\\-html\\+django span.nf").each(function(i, elem) {
             var filtername = $(elem).text();
             if ($.inArray(filtername, django_template_builtins.tfilters) != -1) {
                 var fragment = filtername.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + filtername + "</a>");
             }
        });
    });
})(jQuery);
</script>


  </head>
  <body>

    <div class="document">
  <div id="custom-doc" class="yui-t6">
    <div id="hd">
      <h1><a href="../../../../index.html">Django 1.7.8.dev20150401230226 documentation</a></h1>
      <div id="global-nav">
        <a title="Home page" href="../../../../index.html">Home</a>  |
        <a title="Table of contents" href="../../../../contents.html">Table of contents</a>  |
        <a title="Global index" href="../../../../genindex.html">Index</a>  |
        <a title="Module index" href="../../../../py-modindex.html">Modules</a>
      </div>
      <div class="nav">
    <a href="../../../index.html" title="Module code" accesskey="U">up</a></div>
    </div>

    <div id="bd">
      <div id="yui-main">
        <div class="yui-b">
          <div class="yui-g" id="_modules-django-contrib-comments-forms">
            
  <h1>Source code for django.contrib.comments.forms</h1><div class="highlight"><pre>
<span class="kn">import</span> <span class="nn">time</span>
<span class="kn">from</span> <span class="nn">django</span> <span class="kn">import</span> <span class="n">forms</span>
<span class="kn">from</span> <span class="nn">django.forms.utils</span> <span class="kn">import</span> <span class="n">ErrorDict</span>
<span class="kn">from</span> <span class="nn">django.conf</span> <span class="kn">import</span> <span class="n">settings</span>
<span class="kn">from</span> <span class="nn">django.contrib.contenttypes.models</span> <span class="kn">import</span> <span class="n">ContentType</span>
<span class="kn">from</span> <span class="nn">django.contrib.comments.models</span> <span class="kn">import</span> <span class="n">Comment</span>
<span class="kn">from</span> <span class="nn">django.utils.crypto</span> <span class="kn">import</span> <span class="n">salted_hmac</span><span class="p">,</span> <span class="n">constant_time_compare</span>
<span class="kn">from</span> <span class="nn">django.utils.encoding</span> <span class="kn">import</span> <span class="n">force_text</span>
<span class="kn">from</span> <span class="nn">django.utils.text</span> <span class="kn">import</span> <span class="n">get_text_list</span>
<span class="kn">from</span> <span class="nn">django.utils</span> <span class="kn">import</span> <span class="n">timezone</span>
<span class="kn">from</span> <span class="nn">django.utils.translation</span> <span class="kn">import</span> <span class="n">ungettext</span><span class="p">,</span> <span class="n">ugettext</span><span class="p">,</span> <span class="n">ugettext_lazy</span> <span class="k">as</span> <span class="n">_</span>

<span class="n">COMMENT_MAX_LENGTH</span> <span class="o">=</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">settings</span><span class="p">,</span><span class="s">&#39;COMMENT_MAX_LENGTH&#39;</span><span class="p">,</span> <span class="mi">3000</span><span class="p">)</span>

<div class="viewcode-block" id="CommentSecurityForm"><a class="viewcode-back" href="../../../../ref/contrib/comments/forms.html#django.contrib.comments.forms.CommentSecurityForm">[docs]</a><span class="k">class</span> <span class="nc">CommentSecurityForm</span><span class="p">(</span><span class="n">forms</span><span class="o">.</span><span class="n">Form</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Handles the security aspects (anti-spoofing) for comment forms.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">content_type</span>  <span class="o">=</span> <span class="n">forms</span><span class="o">.</span><span class="n">CharField</span><span class="p">(</span><span class="n">widget</span><span class="o">=</span><span class="n">forms</span><span class="o">.</span><span class="n">HiddenInput</span><span class="p">)</span>
    <span class="n">object_pk</span>     <span class="o">=</span> <span class="n">forms</span><span class="o">.</span><span class="n">CharField</span><span class="p">(</span><span class="n">widget</span><span class="o">=</span><span class="n">forms</span><span class="o">.</span><span class="n">HiddenInput</span><span class="p">)</span>
    <span class="n">timestamp</span>     <span class="o">=</span> <span class="n">forms</span><span class="o">.</span><span class="n">IntegerField</span><span class="p">(</span><span class="n">widget</span><span class="o">=</span><span class="n">forms</span><span class="o">.</span><span class="n">HiddenInput</span><span class="p">)</span>
    <span class="n">security_hash</span> <span class="o">=</span> <span class="n">forms</span><span class="o">.</span><span class="n">CharField</span><span class="p">(</span><span class="n">min_length</span><span class="o">=</span><span class="mi">40</span><span class="p">,</span> <span class="n">max_length</span><span class="o">=</span><span class="mi">40</span><span class="p">,</span> <span class="n">widget</span><span class="o">=</span><span class="n">forms</span><span class="o">.</span><span class="n">HiddenInput</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">target_object</span><span class="p">,</span> <span class="n">data</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">initial</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">target_object</span> <span class="o">=</span> <span class="n">target_object</span>
        <span class="k">if</span> <span class="n">initial</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span>
            <span class="n">initial</span> <span class="o">=</span> <span class="p">{}</span>
        <span class="n">initial</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">generate_security_data</span><span class="p">())</span>
        <span class="nb">super</span><span class="p">(</span><span class="n">CommentSecurityForm</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">__init__</span><span class="p">(</span><span class="n">data</span><span class="o">=</span><span class="n">data</span><span class="p">,</span> <span class="n">initial</span><span class="o">=</span><span class="n">initial</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">security_errors</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Return just those errors associated with security&quot;&quot;&quot;</span>
        <span class="n">errors</span> <span class="o">=</span> <span class="n">ErrorDict</span><span class="p">()</span>
        <span class="k">for</span> <span class="n">f</span> <span class="ow">in</span> <span class="p">[</span><span class="s">&quot;honeypot&quot;</span><span class="p">,</span> <span class="s">&quot;timestamp&quot;</span><span class="p">,</span> <span class="s">&quot;security_hash&quot;</span><span class="p">]:</span>
            <span class="k">if</span> <span class="n">f</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">errors</span><span class="p">:</span>
                <span class="n">errors</span><span class="p">[</span><span class="n">f</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">errors</span><span class="p">[</span><span class="n">f</span><span class="p">]</span>
        <span class="k">return</span> <span class="n">errors</span>

    <span class="k">def</span> <span class="nf">clean_security_hash</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Check the security hash.&quot;&quot;&quot;</span>
        <span class="n">security_hash_dict</span> <span class="o">=</span> <span class="p">{</span>
            <span class="s">&#39;content_type&#39;</span> <span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">data</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">&quot;content_type&quot;</span><span class="p">,</span> <span class="s">&quot;&quot;</span><span class="p">),</span>
            <span class="s">&#39;object_pk&#39;</span> <span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">data</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">&quot;object_pk&quot;</span><span class="p">,</span> <span class="s">&quot;&quot;</span><span class="p">),</span>
            <span class="s">&#39;timestamp&#39;</span> <span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">data</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">&quot;timestamp&quot;</span><span class="p">,</span> <span class="s">&quot;&quot;</span><span class="p">),</span>
        <span class="p">}</span>
        <span class="n">expected_hash</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">generate_security_hash</span><span class="p">(</span><span class="o">**</span><span class="n">security_hash_dict</span><span class="p">)</span>
        <span class="n">actual_hash</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">cleaned_data</span><span class="p">[</span><span class="s">&quot;security_hash&quot;</span><span class="p">]</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">constant_time_compare</span><span class="p">(</span><span class="n">expected_hash</span><span class="p">,</span> <span class="n">actual_hash</span><span class="p">):</span>
            <span class="k">raise</span> <span class="n">forms</span><span class="o">.</span><span class="n">ValidationError</span><span class="p">(</span><span class="s">&quot;Security hash check failed.&quot;</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">actual_hash</span>

    <span class="k">def</span> <span class="nf">clean_timestamp</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Make sure the timestamp isn&#39;t too far (&gt; 2 hours) in the past.&quot;&quot;&quot;</span>
        <span class="n">ts</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">cleaned_data</span><span class="p">[</span><span class="s">&quot;timestamp&quot;</span><span class="p">]</span>
        <span class="k">if</span> <span class="n">time</span><span class="o">.</span><span class="n">time</span><span class="p">()</span> <span class="o">-</span> <span class="n">ts</span> <span class="o">&gt;</span> <span class="p">(</span><span class="mi">2</span> <span class="o">*</span> <span class="mi">60</span> <span class="o">*</span> <span class="mi">60</span><span class="p">):</span>
            <span class="k">raise</span> <span class="n">forms</span><span class="o">.</span><span class="n">ValidationError</span><span class="p">(</span><span class="s">&quot;Timestamp check failed&quot;</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">ts</span>

    <span class="k">def</span> <span class="nf">generate_security_data</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Generate a dict of security data for &quot;initial&quot; data.&quot;&quot;&quot;</span>
        <span class="n">timestamp</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">time</span><span class="o">.</span><span class="n">time</span><span class="p">())</span>
        <span class="n">security_dict</span> <span class="o">=</span>   <span class="p">{</span>
            <span class="s">&#39;content_type&#39;</span>  <span class="p">:</span> <span class="nb">str</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">target_object</span><span class="o">.</span><span class="n">_meta</span><span class="p">),</span>
            <span class="s">&#39;object_pk&#39;</span>     <span class="p">:</span> <span class="nb">str</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">target_object</span><span class="o">.</span><span class="n">_get_pk_val</span><span class="p">()),</span>
            <span class="s">&#39;timestamp&#39;</span>     <span class="p">:</span> <span class="nb">str</span><span class="p">(</span><span class="n">timestamp</span><span class="p">),</span>
            <span class="s">&#39;security_hash&#39;</span> <span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">initial_security_hash</span><span class="p">(</span><span class="n">timestamp</span><span class="p">),</span>
        <span class="p">}</span>
        <span class="k">return</span> <span class="n">security_dict</span>

    <span class="k">def</span> <span class="nf">initial_security_hash</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">timestamp</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Generate the initial security hash from self.content_object</span>
<span class="sd">        and a (unix) timestamp.</span>
<span class="sd">        &quot;&quot;&quot;</span>

        <span class="n">initial_security_dict</span> <span class="o">=</span> <span class="p">{</span>
            <span class="s">&#39;content_type&#39;</span> <span class="p">:</span> <span class="nb">str</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">target_object</span><span class="o">.</span><span class="n">_meta</span><span class="p">),</span>
            <span class="s">&#39;object_pk&#39;</span> <span class="p">:</span> <span class="nb">str</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">target_object</span><span class="o">.</span><span class="n">_get_pk_val</span><span class="p">()),</span>
            <span class="s">&#39;timestamp&#39;</span> <span class="p">:</span> <span class="nb">str</span><span class="p">(</span><span class="n">timestamp</span><span class="p">),</span>
          <span class="p">}</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">generate_security_hash</span><span class="p">(</span><span class="o">**</span><span class="n">initial_security_dict</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">generate_security_hash</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">content_type</span><span class="p">,</span> <span class="n">object_pk</span><span class="p">,</span> <span class="n">timestamp</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Generate a HMAC security hash from the provided info.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">info</span> <span class="o">=</span> <span class="p">(</span><span class="n">content_type</span><span class="p">,</span> <span class="n">object_pk</span><span class="p">,</span> <span class="n">timestamp</span><span class="p">)</span>
        <span class="n">key_salt</span> <span class="o">=</span> <span class="s">&quot;django.contrib.forms.CommentSecurityForm&quot;</span>
        <span class="n">value</span> <span class="o">=</span> <span class="s">&quot;-&quot;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">info</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">salted_hmac</span><span class="p">(</span><span class="n">key_salt</span><span class="p">,</span> <span class="n">value</span><span class="p">)</span><span class="o">.</span><span class="n">hexdigest</span><span class="p">()</span>
</div>
<div class="viewcode-block" id="CommentDetailsForm"><a class="viewcode-back" href="../../../../ref/contrib/comments/forms.html#django.contrib.comments.forms.CommentDetailsForm">[docs]</a><span class="k">class</span> <span class="nc">CommentDetailsForm</span><span class="p">(</span><span class="n">CommentSecurityForm</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Handles the specific details of the comment (name, comment, etc.).</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">name</span>          <span class="o">=</span> <span class="n">forms</span><span class="o">.</span><span class="n">CharField</span><span class="p">(</span><span class="n">label</span><span class="o">=</span><span class="n">_</span><span class="p">(</span><span class="s">&quot;Name&quot;</span><span class="p">),</span> <span class="n">max_length</span><span class="o">=</span><span class="mi">50</span><span class="p">)</span>
    <span class="n">email</span>         <span class="o">=</span> <span class="n">forms</span><span class="o">.</span><span class="n">EmailField</span><span class="p">(</span><span class="n">label</span><span class="o">=</span><span class="n">_</span><span class="p">(</span><span class="s">&quot;Email address&quot;</span><span class="p">))</span>
    <span class="n">url</span>           <span class="o">=</span> <span class="n">forms</span><span class="o">.</span><span class="n">URLField</span><span class="p">(</span><span class="n">label</span><span class="o">=</span><span class="n">_</span><span class="p">(</span><span class="s">&quot;URL&quot;</span><span class="p">),</span> <span class="n">required</span><span class="o">=</span><span class="bp">False</span><span class="p">)</span>
    <span class="n">comment</span>       <span class="o">=</span> <span class="n">forms</span><span class="o">.</span><span class="n">CharField</span><span class="p">(</span><span class="n">label</span><span class="o">=</span><span class="n">_</span><span class="p">(</span><span class="s">&#39;Comment&#39;</span><span class="p">),</span> <span class="n">widget</span><span class="o">=</span><span class="n">forms</span><span class="o">.</span><span class="n">Textarea</span><span class="p">,</span>
                                    <span class="n">max_length</span><span class="o">=</span><span class="n">COMMENT_MAX_LENGTH</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">get_comment_object</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Return a new (unsaved) comment object based on the information in this</span>
<span class="sd">        form. Assumes that the form is already validated and will throw a</span>
<span class="sd">        ValueError if not.</span>

<span class="sd">        Does not set any of the fields that would come from a Request object</span>
<span class="sd">        (i.e. ``user`` or ``ip_address``).</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">is_valid</span><span class="p">():</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s">&quot;get_comment_object may only be called on valid forms&quot;</span><span class="p">)</span>

        <span class="n">CommentModel</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_comment_model</span><span class="p">()</span>
        <span class="n">new</span> <span class="o">=</span> <span class="n">CommentModel</span><span class="p">(</span><span class="o">**</span><span class="bp">self</span><span class="o">.</span><span class="n">get_comment_create_data</span><span class="p">())</span>
        <span class="n">new</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">check_for_duplicate_comment</span><span class="p">(</span><span class="n">new</span><span class="p">)</span>

        <span class="k">return</span> <span class="n">new</span>

    <span class="k">def</span> <span class="nf">get_comment_model</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Get the comment model to create with this form. Subclasses in custom</span>
<span class="sd">        comment apps should override this, get_comment_create_data, and perhaps</span>
<span class="sd">        check_for_duplicate_comment to provide custom comment models.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="n">Comment</span>

    <span class="k">def</span> <span class="nf">get_comment_create_data</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Returns the dict of data to be used to create a comment. Subclasses in</span>
<span class="sd">        custom comment apps that override get_comment_model can override this</span>
<span class="sd">        method to add extra fields onto a custom comment model.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="nb">dict</span><span class="p">(</span>
            <span class="n">content_type</span><span class="o">=</span><span class="n">ContentType</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">get_for_model</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">target_object</span><span class="p">),</span>
            <span class="n">object_pk</span><span class="o">=</span><span class="n">force_text</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">target_object</span><span class="o">.</span><span class="n">_get_pk_val</span><span class="p">()),</span>
            <span class="n">user_name</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">cleaned_data</span><span class="p">[</span><span class="s">&quot;name&quot;</span><span class="p">],</span>
            <span class="n">user_email</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">cleaned_data</span><span class="p">[</span><span class="s">&quot;email&quot;</span><span class="p">],</span>
            <span class="n">user_url</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">cleaned_data</span><span class="p">[</span><span class="s">&quot;url&quot;</span><span class="p">],</span>
            <span class="n">comment</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">cleaned_data</span><span class="p">[</span><span class="s">&quot;comment&quot;</span><span class="p">],</span>
            <span class="n">submit_date</span><span class="o">=</span><span class="n">timezone</span><span class="o">.</span><span class="n">now</span><span class="p">(),</span>
            <span class="n">site_id</span><span class="o">=</span><span class="n">settings</span><span class="o">.</span><span class="n">SITE_ID</span><span class="p">,</span>
            <span class="n">is_public</span><span class="o">=</span><span class="bp">True</span><span class="p">,</span>
            <span class="n">is_removed</span><span class="o">=</span><span class="bp">False</span><span class="p">,</span>
        <span class="p">)</span>

    <span class="k">def</span> <span class="nf">check_for_duplicate_comment</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">new</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Check that a submitted comment isn&#39;t a duplicate. This might be caused</span>
<span class="sd">        by someone posting a comment twice. If it is a dup, silently return the *previous* comment.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">possible_duplicates</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_comment_model</span><span class="p">()</span><span class="o">.</span><span class="n">_default_manager</span><span class="o">.</span><span class="n">using</span><span class="p">(</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">target_object</span><span class="o">.</span><span class="n">_state</span><span class="o">.</span><span class="n">db</span>
        <span class="p">)</span><span class="o">.</span><span class="n">filter</span><span class="p">(</span>
            <span class="n">content_type</span><span class="o">=</span><span class="n">new</span><span class="o">.</span><span class="n">content_type</span><span class="p">,</span>
            <span class="n">object_pk</span><span class="o">=</span><span class="n">new</span><span class="o">.</span><span class="n">object_pk</span><span class="p">,</span>
            <span class="n">user_name</span><span class="o">=</span><span class="n">new</span><span class="o">.</span><span class="n">user_name</span><span class="p">,</span>
            <span class="n">user_email</span><span class="o">=</span><span class="n">new</span><span class="o">.</span><span class="n">user_email</span><span class="p">,</span>
            <span class="n">user_url</span><span class="o">=</span><span class="n">new</span><span class="o">.</span><span class="n">user_url</span><span class="p">,</span>
        <span class="p">)</span>
        <span class="k">for</span> <span class="n">old</span> <span class="ow">in</span> <span class="n">possible_duplicates</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">old</span><span class="o">.</span><span class="n">submit_date</span><span class="o">.</span><span class="n">date</span><span class="p">()</span> <span class="o">==</span> <span class="n">new</span><span class="o">.</span><span class="n">submit_date</span><span class="o">.</span><span class="n">date</span><span class="p">()</span> <span class="ow">and</span> <span class="n">old</span><span class="o">.</span><span class="n">comment</span> <span class="o">==</span> <span class="n">new</span><span class="o">.</span><span class="n">comment</span><span class="p">:</span>
                <span class="k">return</span> <span class="n">old</span>

        <span class="k">return</span> <span class="n">new</span>

    <span class="k">def</span> <span class="nf">clean_comment</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        If COMMENTS_ALLOW_PROFANITIES is False, check that the comment doesn&#39;t</span>
<span class="sd">        contain anything in PROFANITIES_LIST.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">comment</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">cleaned_data</span><span class="p">[</span><span class="s">&quot;comment&quot;</span><span class="p">]</span>
        <span class="k">if</span> <span class="n">settings</span><span class="o">.</span><span class="n">COMMENTS_ALLOW_PROFANITIES</span> <span class="o">==</span> <span class="bp">False</span><span class="p">:</span>
            <span class="n">bad_words</span> <span class="o">=</span> <span class="p">[</span><span class="n">w</span> <span class="k">for</span> <span class="n">w</span> <span class="ow">in</span> <span class="n">settings</span><span class="o">.</span><span class="n">PROFANITIES_LIST</span> <span class="k">if</span> <span class="n">w</span> <span class="ow">in</span> <span class="n">comment</span><span class="o">.</span><span class="n">lower</span><span class="p">()]</span>
            <span class="k">if</span> <span class="n">bad_words</span><span class="p">:</span>
                <span class="k">raise</span> <span class="n">forms</span><span class="o">.</span><span class="n">ValidationError</span><span class="p">(</span><span class="n">ungettext</span><span class="p">(</span>
                    <span class="s">&quot;Watch your mouth! The word </span><span class="si">%s</span><span class="s"> is not allowed here.&quot;</span><span class="p">,</span>
                    <span class="s">&quot;Watch your mouth! The words </span><span class="si">%s</span><span class="s"> are not allowed here.&quot;</span><span class="p">,</span>
                    <span class="nb">len</span><span class="p">(</span><span class="n">bad_words</span><span class="p">))</span> <span class="o">%</span> <span class="n">get_text_list</span><span class="p">(</span>
                        <span class="p">[</span><span class="s">&#39;&quot;</span><span class="si">%s%s%s</span><span class="s">&quot;&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">i</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="s">&#39;-&#39;</span><span class="o">*</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">i</span><span class="p">)</span><span class="o">-</span><span class="mi">2</span><span class="p">),</span> <span class="n">i</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">])</span>
                         <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">bad_words</span><span class="p">],</span> <span class="n">ugettext</span><span class="p">(</span><span class="s">&#39;and&#39;</span><span class="p">)))</span>
        <span class="k">return</span> <span class="n">comment</span>
</div>
<div class="viewcode-block" id="CommentForm"><a class="viewcode-back" href="../../../../ref/contrib/comments/forms.html#django.contrib.comments.forms.CommentForm">[docs]</a><span class="k">class</span> <span class="nc">CommentForm</span><span class="p">(</span><span class="n">CommentDetailsForm</span><span class="p">):</span>
    <span class="n">honeypot</span>      <span class="o">=</span> <span class="n">forms</span><span class="o">.</span><span class="n">CharField</span><span class="p">(</span><span class="n">required</span><span class="o">=</span><span class="bp">False</span><span class="p">,</span>
                                    <span class="n">label</span><span class="o">=</span><span class="n">_</span><span class="p">(</span><span class="s">&#39;If you enter anything in this field &#39;</span>\
                                            <span class="s">&#39;your comment will be treated as spam&#39;</span><span class="p">))</span>

    <span class="k">def</span> <span class="nf">clean_honeypot</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Check that nothing&#39;s been entered into the honeypot.&quot;&quot;&quot;</span>
        <span class="n">value</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">cleaned_data</span><span class="p">[</span><span class="s">&quot;honeypot&quot;</span><span class="p">]</span>
        <span class="k">if</span> <span class="n">value</span><span class="p">:</span>
            <span class="k">raise</span> <span class="n">forms</span><span class="o">.</span><span class="n">ValidationError</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">fields</span><span class="p">[</span><span class="s">&quot;honeypot&quot;</span><span class="p">]</span><span class="o">.</span><span class="n">label</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">value</span></div>
</pre></div>

          </div>
        </div>
      </div>
      
        
          <div class="yui-b" id="sidebar">
            
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
  <h3>Browse</h3>
  <ul>
    
    
  </ul>
  <h3>You are here:</h3>
  <ul>
      <li>
        <a href="../../../../index.html">Django 1.7.8.dev20150401230226 documentation</a>
        
          <ul><li><a href="../../../index.html">Module code</a>
        
          <ul><li><a href="../../../django.html">django</a>
        
          <ul><li><a href="../comments.html">django.contrib.comments</a>
        
        <ul><li>django.contrib.comments.forms</li></ul>
        </li></ul></li></ul></li></ul>
      </li>
  </ul>

<div id="searchbox" style="display: none">
  <h3>Quick search</h3>
    <form class="search" action="../../../../search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    <p class="searchtip" style="font-size: 90%">
    Enter search terms or a module, class or function name.
    </p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
              <h3>Last update:</h3>
              <p class="topless">Apr 02, 2015</p>
          </div>
        
      
    </div>

    <div id="ft">
      <div class="nav">
    <a href="../../../index.html" title="Module code" accesskey="U">up</a></div>
    </div>
  </div>

      <div class="clearer"></div>
    </div>
  </body>
</html>